<?php
$relPath='../../pinc/';
require_once $relPath."dpinit.php";
require_once $relPath."dp_main.inc";

// get variables passed into page
$projectid      = ArgProjectId();
$pagename       = ArgPageName();
$imagefile      = Arg("imagefile");
$page           = new DpPage($projectid, $pagename);
$project        = new DpProject($projectid);
$pct            = Arg("pct", "100");

$title = _("Display Image for Page {$pagename}");

$previous   = _("Previous");
$next       = _("Next");


$script = "<script type='text/javascript'>
    var pageimg;
    var jumpto;
    var pctbox;
    var btnnext1;
    var btnprev1;

    function $(id) {
        return document.getElementById(id);
    }

    function init() {
        pageimg  = $('pageimg');
        jumpto   = $('jumpto');
        pctbox   = $('pctbox');
        btnnext1 = $('btnnext1');
        btnprev1 = $('btnprev1');

        jumpto.onchange = set_image;
        pageimg.style.visibility = 'visible';
        setviewpct();
    }

    function donext() {
        var i = jumpto.selectedIndex;
        if(i < jumpto.length) {
            jumpto.selectedIndex++;
            set_image();
        }
    }

    function doprev() {
        var i = jumpto.selectedIndex;
        if(i > 0) {
            jumpto.selectedIndex--;
            set_image();
        }
    }

    function set_image() {
        pageimg.style.visibility = 'hidden';
        document.imgform.pagename.value 
                = jumpto.value.replace(/\..*$/, '');
        pageimg.src = '{$code_url}/imgsrv.php"
            ."?projectid={$projectid}"
            ."&pagename='+jumpto.value;
        btnnext1.disabled =
            (jumpto.selectedIndex >= (jumpto.length - 1));
        btnprev1.disabled = (jumpto.selectedIndex <= 0);
        document.getElementById('pagetitle').innerHTML
            = document.imgform.projectname.value 
                + '(' + document.imgform.pagename.value + ')';
    }

    function eImgLoad() {
        if(pageimg)
            pageimg.style.visibility = 'visible';
        divimage.scrollTop = '0px';
    }

    function setviewpct() {
        var date = new Date();
        date.setTime(date.getTime() + 365*24*60*60*1000);
        document.cookie = 'viewpct=' 
                + pctbox.value 
                + ';' 
                + ' expires=' 
                + date.toGMTString() 
                + '; path=/';
    }

    function getviewpct() {
        var c = document.cookie.split(';');
        for(var i = 0; i < c.length; i++) {
            var v = c[i].split('=');
            if(v[0] && v[0] == 'viewpct') {
                return v[1] ? v[1] : '100';
            }
            return '100';
        }
	}

</script>\n";

$style = "
<style>
    body { 
/*
        position: absolute;
        height: 100%;
        width: 100%;
        margin: 0;
*/
        text-align: center;
        font-size: .8em;
        overflow: hidden;
    }
    h3        { margin: 0; padding: 0; }
    #hdrbar   { margin: auto; overflow: hidden; }
    #divimage  {
        position: absolute; 
        height: 94.5%; 
        width:100%;
        overflow: auto;
        top: 4em;
        bottom: 0;
    }
    #linksdiv { float: left; margin-left: 3em;}
    #pageimg  { border: 1px solid gray;}
</style>\n";

$imageurl = url_for_image_server($projectid, $pagename);

$pgselect = 
    "<select style='margin: .3em;' name='jumpto' id='jumpto'>\n";
foreach($project->PageRows() as $row) {
    $pgname   = $row['fileid'];
    $filename = $row['image'];

    $pgselect .= "<option value='{$pgname}'"
        . ($pgname === $pagename ? " selected" : "")
        .">{$pgname}</option>\n";
}
$pgselect .= "</select>\n";

echo "<!doctype html>
<html>
<head>
<meta charset='utf-8'>
".favicon()."
<title>$title</title>
{$script}
{$style}
</head>
<body onload='init()'>
<form id='imgform' name='imgform'>
<div id='linksdiv'>"
. link_to_project($projectid, "Project page")."<br>"
. link_to_page_detail($projectid, "Page list") 
."</div>
<div id='hdrbar'>
<h3 id='pagetitle'>{$project->Title()} ({$pagename})</h3>
<input type='hidden' name='projectid' value='$projectid'>
<input type='hidden' name='projectname' value='{$project->Title()}'>
<input type='hidden' name='pagename' id='pagename' value='$pagename'>
<input style='margin: .3em; width: 2em;' type='text'
    maxlength='3' name='pct' id='pctbox' value='$pct'>%
<input style='margin: .3em;' type='submit' value='Resize'>
&nbsp;|&nbsp; "._("View page:")."
{$pgselect}&nbsp;|&nbsp;"
."&nbsp;|&nbsp;
<input style='margin: .3em;' type='button' id='btnprev1'
            value='$previous' onclick='doprev()'>
<input style='margin: .3em;' type='button' id='btnnext1'
            value='$next' onclick='donext()'>
</div>
<div id='divimage'>
<img id='pageimg' style='height: {$pct}%;'
    alt='' src='$imageurl' onload='eImgLoad()'>
</div>
</form>
    </body>
</html>";

// vim: sw=4 ts=4 expandtab
?>
